<?php
Globals::requireClass('Controller');
Globals::requireClass('Alipay');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Trysee');
Globals::requireTable('Tryseetype');
Globals::requireTable('Tryseeorder');
Globals::requireTable('Vmoney');
Globals::requireTable('Vlevel');

class TryseeController extends Controller
{
    protected $UserTable;
    protected $TryseeTable;
    protected $TryseeTypeTable;
    protected $TryseeOrderTable;
    protected $VmoneyTable;
    protected $VlevelTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->UserTable = new UserTable($config);
		$this->TryseeTable = new TryseeTable($config);
		$this->TryseeTypeTable = new TryseetypeTable($config);
		$this->TryseeOrderTable = new TryseeorderTable($config);
		$this->VmoneyTable = new VmoneyTable($config);
		$this->VlevelTable = new VlevelTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	//试看视频类型列表（四期）
	public function tryseetypeAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $data = array();
	    $type_data = $this->TryseeTypeTable->listAll(array('status'=>1));
	    foreach ($type_data as $v){
	        $tmp['title'] = $v['title'];
	        $tmp['type'] = $v['id'];
	        $data[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>'0','list'=>$data));
	    exit();
	}
	
	//试看视频列表（四期）
	public function tryseelistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $type = $this->getIntParam('type');
	    
	    $data = array();
	    $type_data = $this->TryseeTable->listAll(array('status'=>1,'type'=>$type),'rank ASC');
	    foreach ($type_data as $v){
	        $tmp['title'] = $v['title'];
	        $tmp['type'] = $v['type'];
	        $tmp['describe'] = $v['remark'];
	        $tmp['price'] = $v['price'];
	        $tmp['id'] = $v['id'];
	        $tmp['url'] = $v['url'];
	        $tmp['icon'] = $v['icon'];
	        
	        $is_buy = $this->TryseeOrderTable->getRow(array('status'=>2,'uid'=>$uid,'trysee_id'=>$v['id']));
	        if(isset($is_buy['id']) && $is_buy['id'] > 0){
	            $tmp['purchased'] = 1;
	            $tmp['time'] = date('Y-m-d',$is_buy['endtime']);
	        }else{
	            $tmp['purchased'] = 0;
	            $tmp['time'] = '';
	        }
	        
	        $data[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>'0','list'=>$data));
	    exit();
	}
	
	//试看视频下单（四期）
	public function buytryseeAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    $user = $this->UserTable->getRow($uid);
	    if($uid < 1 || $user['id'] < 1){
	        echo json_encode(array('returncode'=>-1,'orderid'=>'','price'=>'','title'=>''));
	        exit();
	    }
	    
	    $time = time();
	    $id = $this->getIntParam('id');
	    $trysee = $this->TryseeTable->getRow($id);
	    if(empty($trysee['id']) || $trysee['status'] != 1){
	        echo json_encode(array('returncode'=>-2,'orderid'=>'','title'=>''));
	        exit();
	    }
	    
	    $data['orderid'] = substr($time, -9) . mt_rand(10, 99);
	    $data['uid'] = $uid;
	    $data['trysee_id'] = $trysee['id'];
	    $data['createdate'] = date('Y-m-d H:i:s');
	    $data['status'] = 1;
	    $data['money'] = $trysee['price'];
	    $this->TryseeOrderTable->add($data);
	    echo json_encode(array('returncode'=>0,'orderid'=>(string)$data['orderid'],'price'=>$data['money'],'title'=>$trysee['remark']));
	    exit();
	}
	
	
	//试看视频支付成功（四期）
	public function buytryseesucceedAction(){
	    $orderid = $this->getParam('orderid');
	    $payorderid = $this->getParam('payorderid');
	    $paytype = $this->getParam('paytype');
	    $sign = $this->getParam('ordersign');
	    $time = time();
	    
	    $order = $this->TryseeOrderTable->getRow(array('orderid'=>$orderid));
	    $trysee = $this->TryseeTable->getRow($order['trysee_id']);
	    $user = $this->UserTable->getRow($order['uid']);
	    
	    if($user['id'] < 1 || $order['id'] < 1 || $order['status'] != 1 || $trysee['id'] < 1 || $trysee['status'] != 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $paytypevalue = 0;
	    $paytype = empty($paytype) ? 'alipay' : $paytype;
	    if($paytype == 'alipay'){
	        $alipay = new Alipay();
	        $re = $alipay->queryone('',$payorderid);
	        $alipay_money = $re->response->trade->total_fee;
	        if($order['money'] != $alipay_money){
	            echo json_encode(array('returncode'=>-999));
	            exit();
	        }
	        
	        $paytypevalue = 1;
	    }elseif($paytype == 'vmoney'){
	        $vcount = 0;
	        $listvalue = $this->VmoneyTable->listAllWithFields('money',array('uid'=>$user['id']));
	        foreach ($listvalue as $k=>$v){
	            $vcount += $v['money'];
	        }
	        if($vcount < $order['money']){
	            echo json_encode(array('returncode'=>-998));
	            exit();
	        }
	        
	        $paytypevalue = 2;
	    }
	    
	    $data['paytype'] = $paytypevalue;
	    $data['paydate'] = date('Y-m-d H:i:s');
	    $data['payorderid'] = $payorderid;
	    $data['status'] = 2;
	    $data['endtime'] = $time + 86400 * 2;
	    $this->TryseeOrderTable->update($data,$order['id']);
	    
	    $user_data['money'] = $user['money'] + $order['money'];
	    $get_money = $order['money'] * 0.05;
	    $get_money = sprintf("%.2f", $get_money);
	    $get_money = $get_money < 0.01 ? 0.01 : $get_money;
	    $user_data['v_money'] = $user['v_money'] + $get_money;
	    $this->UserTable->update($user_data,$order['uid']);
	    
	    $vmoney_data['uid'] = $user['id'];
	    $vmoney_data['type'] = 1;
	    $vmoney_data['money'] = $get_money;
	    $vmoney_data['remark'] = '购买试看视频获得V币'.$get_money.'，'.$trysee['title'];
	    $this->VmoneyTable->add($vmoney_data);
	    
	    $vlevel_data['uid'] = $user['id'];
	    $vlevel_data['type'] = 1;
	    $vlevel_data['level'] = $get_money;
	    $vlevel_data['remark'] = '购买账号获得V级'.$get_money.'，'.$trysee['title'];
	    $this->VlevelTable->add($vlevel_data);
	    unset($vlevel_data);
	    
	    if($paytype == 'vmoney'){
	        $user_data['v_money'] = $user_data['v_money'] - $order['money'];
	        $this->UserTable->update($user_data,$order['uid']);
	        
	        $vmoney_data['uid'] = $user['id'];
	        $vmoney_data['type'] = 4;
	        $vmoney_data['money'] = (0-$order['money']);
	        $vmoney_data['remark'] = '购买试看视频消耗V币'.$order['money'];
	        $this->VmoneyTable->add($vmoney_data);
	        
	    }
	    
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	
	//试看视频拼接url（四期）
	public function tryseeurlAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','url'=>''));
	        exit();
	    }
	    
	    $id = $this->getIntParam('id');
	    $url = $this->getParam('url');
	    
	    $date = date('Y-m-d H:i:s');
	    
	    $trysee = $this->TryseeTable->getRow($id);
	    $order_data = $this->TryseeOrderTable->getRow(array('status'=>2,'uid'=>$uid,'trysee_id'=>$id));
	    if((int)$trysee['id'] < 1 || (int)$order_data['id'] < 1){
	        echo json_encode(array('returncode'=>'-2','url'=>''));
	        exit();
	    }
	    
	    $newurl = $trysee['plus_url'];
	    
	    echo json_encode(array('returncode'=>'0','url'=>$newurl));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('TryseeController', 'Controller');
